﻿using ChineseChess.Game.AI;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace ChineseChess.Game
{
    public class Pos
    {
        /// <summary>
        /// 当前走棋的阵营,true是红子，false是黑子
        /// </summary>
        public bool CurrectCamp
        {
            get;
            set;
        }

        /// <summary>
        /// 改变走棋阵营
        /// </summary>
        public void ChangeCamp()
        {
            CurrectCamp = !CurrectCamp;
        }

        /// <summary>
        /// 棋子列表
        /// </summary>
        public List<Piece> PieceList = new List<Piece>();

        /// <summary>
        /// 根据棋盘获取某点的子
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="pies"></param>
        /// <returns></returns>
        public Piece getPieByXy(int x, int y, List<Piece> pies)
        {
            for (int i = 0; i < pies.Count; i++)
            {
                var pie = pies[i];
                if (pie.X == x && pie.Y == y)
                {
                    return pie;
                }
            }
            return null;
        }
        /// <summary>
        /// 评估当前局面得分
        /// </summary>
        /// <returns></returns>
        public int evaluate()
        {
            return Ai.getCampScore(this.PieceList, CurrectCamp);
        }

        public List<Moves> generateMoves()
        {
            List<Moves> mv = new List<Moves>();

            for (int i = 0; i < PieceList.Count; i++)
            {   //遍历走子
                var pie = PieceList[i];

                if (pie.isRed == this.CurrectCamp) //当前走的子,第一步， 黑子走
                {
                    List<Point> point = pie.rule.getNextPos(pie, PieceList); //该子下一步可走的点

                    for (int j = 0; j < point.Count; j++)
                    {
                        mv.Add(new Moves(pie, point[j]));
                    }
                }
            }

            return mv;
        }

        private Piece movedPie;
        public bool makeMove(Moves mv)
        {
            lastMv = new Moves(Ai.CopyPiece(mv.pie), mv.p);
            movedPie = Ai.clearPiece(this.PieceList, (int)mv.p.X, (int)mv.p.Y, !CurrectCamp);
            mv.pie.X = (int) mv.p.X;
            mv.pie.Y = (int)mv.p.Y;
            this.ChangeCamp();
            return true;
        }

        private Moves lastMv;
        public void undoMakeMove(Moves mv)
        {
            mv.pie.X =  lastMv.pie.X;
            mv.pie.Y = lastMv.pie.Y;
            if (movedPie != null)
            {
                PieceList.Add(movedPie);
            }
            this.ChangeCamp();
        }
    }

    public class Moves
    {
        public Piece pie;
        public Point p;
        public int score;
        public Moves()
        { 
        }
        public Moves(Piece pie,Point p)
        {
            this.pie = pie;
            this.p = p;
        }
    }
}
